//
//  ForfotFormViewController.swift
//  iModern
//
//  Created by 立雄 王 on 15/9/1.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class SubmitReportFormViewController: FormViewController {
    
    var report = NSMutableDictionary()
    var reportManager = ReportManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = " "
        
        reportManager.parameterManager = appDelegate.parameterManager
        
        initializeForm()
        initBackButton()
        initRightBarButtonItem()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor
        
        form = XLFormDescriptor(title: " ")
        
        section = XLFormSectionDescriptor.formSectionWithTitle("")
        section.footerTitle = ""
        form.addFormSection(section)
        
        row = XLFormRowDescriptor(tag: "type", rowType: XLFormRowDescriptorTypeSelectorPush, title: "举报原因")
        let a = ["色情低俗","广告骚扰","诱导分享","谣言","政治敏感","违法（暴力恐怖、违禁品等）","其它（收集隐私信息等）","侵权（诽谤、抄袭、冒用等）"]
        row.selectorOptions = a
        section.addFormRow(row)
        
        row = XLFormRowDescriptor(tag: "description", rowType: XLFormRowDescriptorTypeTextView, title: "补充说明")
        section.addFormRow(row)
        
        self.form = form
    }
    
    func initRightBarButtonItem()
    {
        let button: UIButton = UIButton(type: UIButtonType.System) ;
        button.frame = CGRectMake(0, 0, 80, 30)
        button.setTitle("发送举报", forState: UIControlState.Normal)
        button.setTitleColor(UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("publishButtonOnTaps:"), forControlEvents: UIControlEvents.TouchUpInside)
        let showButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = showButton
    }
    
    @IBAction func publishButtonOnTaps(sender: AnyObject)
    {
        //var appointments = NSMutableDictionary()
        
        let type = self.formValues()["type"] as? String
        if type == nil
        {
            self.alertWarningMessage("请选择举报原因")
            return
        }
        else
        {
            report["type"] = self.formValues()["type"]
        }

        let description = self.formValues()["description"] as? String
        if description == nil
        {
            report["description"] = "无"
        }
        else
        {
            report["description"] = self.formValues()["type"]
        }
        
        let rs = reportManager.submitReport(report as [NSObject:AnyObject])
    
//        if rs == nil
//        {
//            self.alertNetworkConnectionException()
//            return
//        }
//        
//        if rs!.hasErrors()
//        {
//            if rs?.errorCode == 3020
//            {
//                self.alertUserStatusException()
//            }
//            else
//            {
//                self.alertErrorMessage(rs!.errorMessage)
//            }
//        }
//        else
//        {
////            self.alertWarningMessage("你的举报已经提交审核，我们会尽快处理，审核结果将通过消息反馈给你。感谢你的支持！")
//            self.navigationController?.popViewControllerAnimated(true)
//            return
//        }
        
        if rs == nil
        {
            self.alertNetworkConnectionException()
        }
        else
        {
            if rs!.hasErrors() && rs!.isEmpty()
            {
                self.alertErrorMessage(rs!.errorMessage)

            }
            else
            {
                self.alertWarningMessage("你的举报已经提交审核，我们会尽快处理，审核结果将通过消息反馈给你。感谢你的支持！")

            }
        }
        self.view.endEditing(true)
//        self.navigationController?.popViewControllerAnimated(true)
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
